package com.hnu.web;

import com.hnu.model.User;

public class RegisterBean extends BaseAction {
	private static final long serialVersionUID = 5562066429589860354L;

	private User user;
	private String repeatPassword;
	private String message;

	public String register()
	{
		if(user.getUserName() == null || user.getUserName().trim().isEmpty()||
				user.getEmail() == null || user.getEmail().trim().isEmpty()||
				this.user.getPassword() == null || this.user.getPassword().trim().isEmpty()||
				this.repeatPassword == null || this.repeatPassword.trim().isEmpty())
		{
			this.message = "所有字段均不允许为空!";
			return "failure";
		}
		
		if(!this.user.getPassword().equals(this.repeatPassword))
		{
			this.message = "两次密码输入必须一致!";
			return "failure";
		}
		
		if(!this.user.getEmail().matches("[0-9a-z_]+@(([0-9a-z]+)[.]){1,2}[a-z]{2,3}"))
		{
			this.message = "邮箱格式不符！";
			return "failure";
		}
		
		user.setPassword(user.getPassword().trim());
		user.setUserName(user.getUserName().trim());
		
		if(!this.servLocator.getUserServ().addUser(user))
		{
			this.message = "该账号已被占用!";
			return "failure";
		}
		
		this.message = "注册成功,请登录!";
		return "success";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getRepeatPassword() {
		return repeatPassword;
	}

	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
